<template>
    <div class="app-container">
        <div class="warp_padding">  
            <div class="head-container">
                <!-- 搜索 -->
                <label id="labelQuery" class="labelQuery">名称: </label>
                <el-input v-model="formDate.groupName" clearable placeholder="校群名称搜索" style="width: 140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>
                
                <span v-if="device!=='mobile'">
                    <label class="labelQuery"  style="padding-left:10px;">校群类型: </label>
                    <el-select v-model="formDate.manageType" clearable placeholder="校群类型搜索" @change="toQuery()" style="width:140px">
                        <el-option
                              v-for="item in manageTypes"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id" style="margin-top:5px">
                        </el-option>
                    </el-select>

                    <label class="labelQuery">县/区: </label>
                    <area-item @getMessage="showMsg"/>
                    

                    <label class="labelQuery"  style="padding-left:10px;">群主名称: </label>
                    <el-select v-model="formDate.schId" clearable  filterable placeholder="群主名称查询" style="width: 165px;" @change="toQuery()">
                        <el-option
                            v-for="item in schIds"
                            :key="item.schId"
                            :label="item.schName"
                            :value="item.schId">
                        </el-option>
                    </el-select>
                    
                               
                    <label class="labelQuery">状态: </label>
                    <el-select v-model="formDate.status" clearable placeholder="状态搜索" @change="toQuery()" style="width:110px">
                        <el-option
                              v-for="item in condition"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id">
                        </el-option>
                    </el-select>
                </span>



                <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
                <!-- 新增 -->
                <div style="display: inline-block;margin: 0px 2px;">
                  <el-button
                    v-if="checkPermission(['group_all','group_add'])"
                    class="filter-item"
                    size="mini"
                    type="primary"
                    icon="el-icon-plus"
                    @click="add">建群</el-button>
                  <eForm ref="form" :is-add="isAdd" :owner='owner'/>
                </div>
            </div>
        </div>

        <!--列表-->
        <el-table :data="lists" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" max-height="650">
            <el-table-column type="selection" width="55"></el-table-column>
            
            <el-table-column type="index" width="45"></el-table-column>
            
            <el-table-column prop="groupName" label="校群名称"  :show-overflow-tooltip="true"></el-table-column>

            <el-table-column prop="ownerSchName" label="群主名称"  :show-overflow-tooltip="true"></el-table-column>

            <el-table-column prop="areaName"  :show-overflow-tooltip="true" label="县/区" ></el-table-column>

            <el-table-column prop="manageType" label="校群类型" min-width="90" :formatter="manageType">
            </el-table-column>

            <!-- <el-table-column prop="num" align="center" width="100px" label="群成员数">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.num }}</el-tag>
                </template>
            </el-table-column> -->

            <el-table-column prop="createtime" label="创建日期" min-width="90" >
                <template slot-scope="scope">
                  <span>{{ scope.row.createtime | formatDate }}</span>
                </template>
            </el-table-column>

            <el-table-column prop="status" label="状态" >
                 <template slot-scope="scope">
                    <el-tag :type="scope.row.status ? 'info' : 'success'">{{ scope.row.status ? '禁用' : '启用' }}</el-tag>
                </template>
            </el-table-column>


            <el-table-column prop="validity" label="有效期" sortable  min-width="90" >
                <template slot-scope="scope">
                  <span v-if="scope.row.validity">{{ scope.row.validity | formatDate }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="250" >
                <template slot-scope="scope">
                    <el-button size="mini" type="warning"   @click='navTo(scope.$index, scope.row.id,scope.row.groupName)' v-if="checkPermission(['group_all','group_edit'])">成员管理</el-button>
                    <el-button size="mini" type="success" v-if="checkPermission(['group_all','group_edit'])" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" v-if="checkPermission(['group_all','group_del'])" @click="handleDel(scope.$index, scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>


        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" size="mini" @click="batchRemove" :disabled="this.sels.length===0" v-if="checkPermission(['sch_all','sch_del'])" class="remove">批量删除</el-button>

            <el-pagination background  @size-change="handleSizeChange"  @current-change="handleCurrentChange"  :current-page.sync="formDate.pageNo" :page-count="pageCount" :page-sizes="[10, 20, 30, 50,100]"  layout="total, sizes,prev, pager, next, jumper" :pager-count="pagerCount" :total="total" class="pagination"></el-pagination>
        </el-col>

    </div>
</template>

<script>


import areaItem from '@/components/area/areaitem'
import checkPermission from '@/utils/permission' 
import { parseTime } from '@/utils/index'
import eForm from './module/form'
import { groupList, groupDel, groupOwner, allSchoolList } from '@/api/request'
import { mapGetters } from 'vuex'
import {formatDate} from '@/utils/formatDate.js';
export default {
	components: { eForm, areaItem  },
    computed: {
        ...mapGetters([
          'device'
        ])
    },
    filters: {
            formatDate(time) {
                let date = new Date(time);
                return formatDate(date, 'yyyy-MM-dd');
            }, 
    },
    data() {
        return {
            schIds:[],
            owner:[],
            pageCount: 0,
            total:0,  
            listLoading: false,
            lists: [],
            sels: [],//列表选中列
            pagerCount:11,   
            formDate:{
                pageNo: 1,  
                pageSize:10,
                groupName:'',
                manageType:'',   //校群类型
                status:'',
                schId:'',
                areaCode:''
            },
            isAdd:true,
            manageTypes: [
                {
                    id: '0',
                    name: '集团校群'
                },
                {
                    id: '1',
                    name: '普通校群'
                },
            ],
            condition:[
                {
                    id: '0',
                    name: '启用'
                },
                {
                    id: '1',
                    name: '禁用'
                },
            ]
        }
    },
    mounted:function(){
        this.init()
        this.allSch()
    },
    methods: {
        parseTime,
        checkPermission,
        showMsg(tir){
            this.formDate.areaCode = tir;
            this.init()
        },   
        manageType(row, column) {
            return row.manageType == 0 ? '集团校群' : row.manageType == 1 ? '普通校群' :'未知';
        },
        selsChange: function (sels) {
                this.sels = sels;
        },
        handleCurrentChange(val) {
            this.formDate.pageNo = val;
            this.init()
        },
        handleSizeChange(val) {
            this.formDate.pageSize = val
            this.init()
        },
        toQuery(){
            this.init()
        },
        add(){
            this.ready()
            this.isAdd = true
            this.$refs.form.form = { 
                id:'',
                groupName:'',
                validity: '', //有效期
                manageType:'',   //校群类型
                status:0,
                groupOwner:'',   //群主id
            },
            this.$refs.form.dialog = true
             this.$refs.form.disabled  = false
        },
        init(){
            this.listLoading = true
            let params = {...this.formDate}
            groupList(params).then(res => { 
                let { list, total } = res
                this.lists = list
                this.total = total
                this.listLoading = false 
            })
        },
        async allSch(){
            try {
                let data = await allSchoolList()
                this.schIds = data.data
            } catch (e) {
                console.log(e)
            }

        },
        async ready(){
            try {
                let res = await groupOwner()
                this.owner = res.data
            }catch(e){
                console.log(e)
            }
        },
        async handleEdit(index, row) {
            this.isAdd = false
            this.$refs.form.form = Object.assign({}, row);
            this.$refs.form.dialog = true
            this.$refs.form.disabled  = true
            // console.log(Object.assign({}, row))
            // try {
            //     let Result = await editTea(id)
            //     this.isAdd = false
            //     this.$refs.edit.dialog = true
            //     let teaType = (Result.data.teaType).toString()
            //     Result.data.teaType = teaType
            //     this.$refs.edit.roles = Result.data.roleList
            //     let dataIntArr = []
            //     let dataStrArr = Result.data.subjectIds.split(","); 
            //     dataIntArr = dataStrArr.map(item => {  
            //         return +item;  
            //     });  
            //     let cardId  = ''
            //     Result.data.subjectIds = dataIntArr
            //     Result.data.cardId = cardId
            //     this.$refs.edit.form = Result.data 
            //    // this.$refs.edit.disabled = false
            // } catch (e) {
            //     console.log(e)
            // }
            
        },
        batchRemove(){
            let ids = this.sels.map(item => item.id);
            console.log(this.sels.length)
            this.$confirm('确认删除选中的记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        this.listLoading = true;
                        groupDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if ( this.lists.length === this.sels.length  && this.formDate.pageNo != 1)
                            this.formDate.pageNo--
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        handleDel(index,ID){
            let ids = [];
            ids.unshift(ID)
            this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        // console.log(ids)
                        this.listLoading = true;
                        groupDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) 
                            this.formDate.pageNo--
                            this.init();
                        })

                    }).catch(() => {
            });
        },

        navTo(index,id,groupName){
            this.$router.push({
                path: '/member/index',
                query: {
                    id: id,
                    groupName:groupName                  
                }
            })
        }
        
       
    },
}

</script>


<style scoped lang="scss">

</style>
